import tensorflow as tf
import numpy as np

FILE = "E:/picture/validation/data.tfrecords-*"
validation_image =[]
validation_label = []
temp = []
tmp = []
def read_my_file_format(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        features={
            'label': tf.FixedLenFeature([], tf.int64),
            'image_raw': tf.FixedLenFeature([], tf.string),
        })

    #labels = tf.cast(features['label'],tf.int32)
    labels = features['label']
    decoded_images = tf.decode_raw(features['image_raw'], tf.uint8)
    # 保存图片，图片格式为pic_*。jpg,'*'表示图片的标签
    image_1 = tf.reshape(decoded_images, [299, 299, 3])  ## reshape 成图片矩阵
    image_1 = tf.image.convert_image_dtype(image_1, dtype=tf.float32)
    return  [image_1,labels]
def get_data(filename):
    files = tf.train.match_filenames_once(filename)
    filename_queue = tf.train.string_input_producer(files, shuffle=False)
    min_after_dequeue = 1
    batch_size = 1
    capacity = min_after_dequeue + 3 * batch_size
    example_list = [read_my_file_format(filename_queue) for _ in range(1)]
    #min_after_dequeue = min_after_dequeue
    image_batch, label_batch = tf.train.batch_join(example_list, batch_size=batch_size,
                                                      capacity=capacity)
    return [image_batch,label_batch]

def all_list(arr):
    result = {}
    for i in set(arr):
        result[i] = arr.count(i)
    return result
with tf.Session() as sess:
    image_batch,label_batch = get_data(FILE)
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    for i in range(358):
        xs, ys = sess.run([image_batch,label_batch])
        validation_image.extend(xs)
        validation_label.extend(ys)

    result = all_list(validation_label)
    print(result)
    print(validation_label)
    state = np.random.get_state()
    np.random.shuffle(validation_image)
    np.random.set_state(state)
    np.random.shuffle(validation_label)
    #processed = np.asarray([validation_image,validation_label])
    # np.save("E:/picture/validation/validation.npy",processed)
    # print("------------")
    coord.request_stop()
    coord.join(threads)
